package com.horse.cloud.framework.general.utils;

import com.horse.cloud.framework.general.enums.DateEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @create : Created by intelliJ IDEA 16.1.3
 * @author : bing.Pan
 * @e-mail : 15923508369@163.com
 * @gmdate : 2017/7/25 11:04
 * @sidesc : 时间工具类，基于jdk版本小于1.8
 */
public class DateUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);

    private static Calendar calendar = Calendar.getInstance();


    /**
     * 时间转字符串形式,缺省格式为：yyyy-MM-dd HH:mm:ss
     * @param date  java LocalDateTime
     * @param patterns 格式
     * @return
     */
    public static String date2String(Date date, String patterns) {

        if (StringUtils.isEmpty(date)){ return null;}

        SimpleDateFormat sdf;
        if(StringUtils.isEmpty(patterns)){
             sdf = new SimpleDateFormat(DateEnums.HYPHEN_YYYYMMddHHmmss.getPatterns());
        } else {
            sdf = new SimpleDateFormat(patterns);
        }
        return sdf.format(date);

    }


    public static Date string2Date(String date){
        SimpleDateFormat df = new SimpleDateFormat(DateEnums.HYPHEN_YYYYMMddHHmmss.getPatterns());
        try {
            return df.parse(date);
        } catch (ParseException e) {
            return null;
        }

    }


    /**
     * 获取系统当前时间的字符串形式（默认返回格式：yyyy-MM-dd HH:mm:ss）
     * @param patterns      返回格式,可为空，为空时返回默认格式
     * @return
     */
    public static String getCurrentTime4String(String patterns){
        SimpleDateFormat format;
        if(StringUtils.isEmpty(patterns)){
            format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            format = new SimpleDateFormat(patterns);
        }

        return format.format(calendar.getTime());
    }


    /**
     * 获取当前日为周几，以周一为每周的第一天
     * @return
     */
    public static int getDayIsToday(){
        return  calendar.get(Calendar.DAY_OF_WEEK) - 1;

    }

    /**
     * 获取当前月的第一天日期字符串格式 (yyyy-MM-dd)
     * @return
     */
    public static String getCurrentMonthFastDay(){

        int mainDay=calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), mainDay, 23, 59, 59);
        SimpleDateFormat sdf = new SimpleDateFormat(DateEnums.HYPHEN_YYYYMMdd.getPatterns());
        return sdf.format(calendar.getTime());
    }

    /**
     *  获取当前月的最后一天日期字符串格式 (yyyy-MM-dd)
     * @return
     */
    public static String getCurrentMonthLastDay(){
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), maxDay, 23, 59, 59);
        SimpleDateFormat sdf = new SimpleDateFormat(DateEnums.HYPHEN_YYYYMMdd.getPatterns());
        return sdf.format(calendar.getTime());

    }


    /**
     * 获取当前月的上月月份
     * @return
     */
    public static String getLastMonth(){
        calendar.add(Calendar.MONTH, -1);
        SimpleDateFormat beforeFormat =  new SimpleDateFormat(DateEnums.HYPHEN_YYYYMM.getPatterns());
        return beforeFormat.format(calendar.getTime());
    }


    /**
     * 获取某月的最后一天
     * @param year   年份
     * @param month  月份
     * @return 返回该月份的最后一天日期的字符串形式
     */
    public static String getLastDayOfMonth(int year,int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,year);
        cal.set(Calendar.MONTH, month-1);
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());

    }


    /**
     * 获取两个时间差
     * @param maxSubtractionDate
     * @param minSumtranctionDate
     * @return
     */
    public static Map<String,Integer> timeDifference(Date maxSubtractionDate, Date minSumtranctionDate){

        if(StringUtils.isEmpty(maxSubtractionDate) || StringUtils.isEmpty(minSumtranctionDate)){
            return null;
        }
        Map<String,Integer> map = new HashMap<>();
        long ms =  maxSubtractionDate.getTime() - minSumtranctionDate.getTime ();
        int day = (int) ( ms / 1000 / 60 / 60 / 24 );
        int hour = (int) ( ms / 1000 / 60 / 60 % 24 );
        int minute = (int) ( ms / 1000 / 60 % 60 );
        int second = (int) ( ms / 1000 % 60 );

        map.put("day",day);
        map.put("hour",hour);
        map.put("minute",minute);
        map.put("second",second);

        return  map;


    }



}
